package com.jackeysun.util

/**
 *   Created by sunxunchao on 2021/11/16.
 */


const val BASE_SYSTEM = 1024

fun Long.convertFileSize(): String {
    val kb = BASE_SYSTEM
    val mb = kb.times(BASE_SYSTEM)
    val gb = mb.times(BASE_SYSTEM)
    return when {
        this >= gb -> {
            String.format("%.1f GB", this.toFloat() / gb)
        }
        this >= mb -> {
            val f = this.toFloat() / mb
            String.format(if (f > 100) "%.0f MB" else "%.1f MB", f)
        }
        this >= kb -> {
            val f = this.toFloat() / kb
            String.format(if (f > 100) "%.0f KB" else "%.1f KB", f)
        }
        else -> String.format("%d B", this)
    }
}